package model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Parser adds class hierarchy for XMLParser and XLSParser so as to improve
 * extendability of code.
 * 
 * @author Tommy Petrilak
 */
public abstract class Parser {
    // Store all the data from the input data file
    @SuppressWarnings("rawtypes")
    private List<ArrayList<Comparable>> myTable;

    // Store the field name of the input data file.
    private List<String> myFieldNames;

    // Store the field data type of the input data file ("Label" or "Number").
    private List<String> myFieldTypes;

    /**
     * 
     * @param file file chosen by user
     * @throws IOException 
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    @SuppressWarnings("rawtypes")
    public Parser (File file) throws IOException {
        myFieldNames = new ArrayList<String>();
        myFieldTypes = new ArrayList<String>();
        myTable = new ArrayList<ArrayList<Comparable>>();
    }

    /**
     * 
     * @param inputDataFile file chosen by user
     * @throws IOException IOException when reading files
     */
    public void loadFile (File inputDataFile) throws IOException {
        readCategories(inputDataFile);
        readData();
    }

    protected abstract void readCategories(File inputDataFile) throws IOException;

    protected abstract void readData();

    /**
     * Determine whether the string could be represented by a number
     * Currently the function supports Double and String
     * 
     * @param string String objects to be determined
     */
    public String determineType (String string) {
        for (int i = string.length() - 1; i >= 0; i--) {
            if (!(Character.isDigit(string.charAt(i)) || (string.charAt(i) == '.'))) {
                return "String";
            }
        }
        return "Double";
    }

    /**
     * @return myFieldNames
     */
    public List<String> getFieldNames () {
        return myFieldNames;
    }

    /**
     * @return myFieldTypes
     */
    public List<String> getFieldTypes () {
        return myFieldTypes;
    }

    /**
     * @return result table
     */
    @SuppressWarnings("rawtypes")
    public List<ArrayList<Comparable>> getTable () {
        return myTable;
    }

    /**
     * retrieve data in Table format
     * @return instance of Table
     */
    public abstract Table retrieveTable ();
}
